{% extends 'assets/base.html' %}
{% load static %}

{% block title %}资产调拨管理 - 固定资产管理系统{% endblock %}

{% block extra_css %}
<style>
    .stats-card {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
        border-radius: 10px;
        padding: 20px;
        margin-bottom: 20px;
        transition: transform 0.3s;
    }
    
    .stats-card:hover {
        transform: translateY(-5px);
    }
    
    .filter-card {
        background: #f8f9fa;
        border-radius: 10px;
        padding: 20px;
        margin-bottom: 20px;
    }
    
    .transfer-table {
        background: white;
        border-radius: 10px;
        overflow: hidden;
        box-shadow: 0 2px 10px rgba(0,0,0,0.1);
    }
    
    .status-badge {
        font-size: 0.8em;
        padding: 4px 8px;
        border-radius: 12px;
    }
    
    .status-draft { background-color: #6c757d; }
    .status-pending { background-color: #ffc107; color: #000; }
    .status-approved { background-color: #28a745; }
    .status-rejected { background-color: #dc3545; }
    .status-in_transit { background-color: #17a2b8; }
    .status-completed { background-color: #28a745; }
    .status-cancelled { background-color: #6c757d; }
    
    .priority-urgent { color: #dc3545; font-weight: bold; }
    .priority-high { color: #fd7e14; }
    .priority-medium { color: #ffc107; }
    .priority-low { color: #28a745; }
</style>
{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h2><i class="fas fa-exchange-alt"></i> 资产调拨管理</h2>
                <div>
                    <a href="{% url 'assets:transfer_create' %}" class="btn btn-primary">
                        <i class="fas fa-plus"></i> 新建调拨申请
                    </a>
                    <a href="{% url 'assets:my_transfer_requests' %}" class="btn btn-outline-info">
                        <i class="fas fa-user"></i> 我的申请
                    </a>
                    <a href="{% url 'assets:pending_approvals' %}" class="btn btn-outline-warning">
                        <i class="fas fa-clock"></i> 待审批
                    </a>
                </div>
            </div>
            
            <!-- 统计信息 -->
            <div class="row mb-4">
                <div class="col-md-3">
                    <div class="stats-card">
                        <h5><i class="fas fa-list"></i> 总调拨数</h5>
                        <h3>{{ stats.total }}</h3>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="stats-card">
                        <h5><i class="fas fa-clock"></i> 待审批</h5>
                        <h3>{{ stats.pending }}</h3>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="stats-card">
                        <h5><i class="fas fa-truck"></i> 调拨中</h5>
                        <h3>{{ stats.in_transit }}</h3>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="stats-card">
                        <h5><i class="fas fa-check"></i> 已完成</h5>
                        <h3>{{ stats.completed }}</h3>
                    </div>
                </div>
            </div>
            
            <!-- 筛选条件 -->
            <div class="filter-card">
                <form method="get" class="row g-3">
                    <div class="col-md-2">
                        <label class="form-label">状态</label>
                        <select name="status" class="form-select">
                            <option value="">全部状态</option>
                            {% for value, label in status_choices %}
                                <option value="{{ value }}" {% if status_filter == value %}selected{% endif %}>{{ label }}</option>
                            {% endfor %}
                        </select>
                    </div>
                    <div class="col-md-2">
                        <label class="form-label">调拨类型</label>
                        <select name="type" class="form-select">
                            <option value="">全部类型</option>
                            {% for value, label in type_choices %}
                                <option value="{{ value }}" {% if type_filter == value %}selected{% endif %}>{{ label }}</option>
                            {% endfor %}
                        </select>
                    </div>
                    <div class="col-md-2">
                        <label class="form-label">开始日期</label>
                        <input type="date" name="date_from" class="form-control" value="{{ date_from }}">
                    </div>
                    <div class="col-md-2">
                        <label class="form-label">结束日期</label>
                        <input type="date" name="date_to" class="form-control" value="{{ date_to }}">
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">搜索</label>
                        <input type="text" name="search" class="form-control" placeholder="搜索调拨单号、标题..." value="{{ search_query }}">
                    </div>
                    <div class="col-md-1">
                        <label class="form-label">&nbsp;</label>
                        <button type="submit" class="btn btn-primary d-block">
                            <i class="fas fa-search"></i>
                        </button>
                    </div>
                </form>
            </div>
            
            <!-- 调拨列表 -->
            <div class="transfer-table">
                <div class="table-responsive">
                    <table class="table table-hover mb-0">
                        <thead class="table-dark">
                            <tr>
                                <th>调拨单号</th>
                                <th>标题</th>
                                <th>类型</th>
                                <th>申请人</th>
                                <th>资产数量</th>
                                <th>目标部门</th>
                                <th>状态</th>
                                <th>优先级</th>
                                <th>创建时间</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for transfer in page_obj %}
                            <tr>
                                <td>
                                    <a href="{% url 'assets:transfer_detail' transfer.pk %}" class="text-decoration-none">
                                        <strong>{{ transfer.transfer_number }}</strong>
                                    </a>
                                </td>
                                <td>
                                    <div class="text-truncate" style="max-width: 200px;" title="{{ transfer.title }}">
                                        {{ transfer.title }}
                                    </div>
                                </td>
                                <td>
                                    <span class="badge bg-secondary">{{ transfer.get_transfer_type_display }}</span>
                                </td>
                                <td>
                                    <i class="fas fa-user"></i> {{ transfer.applicant.username }}
                                    {% if transfer.applicant_department %}
                                        <br><small class="text-muted">{{ transfer.applicant_department.name }}</small>
                                    {% endif %}
                                </td>
                                <td>
                                    <span class="badge bg-info">{{ transfer.asset_count }} 个</span>
                                </td>
                                <td>
                                    {% if transfer.target_department %}
                                        {{ transfer.target_department.name }}
                                    {% else %}
                                        <span class="text-muted">-</span>
                                    {% endif %}
                                </td>
                                <td>
                                    <span class="badge status-{{ transfer.status }} status-badge">
                                        {{ transfer.get_status_display }}
                                    </span>
                                    {% if transfer.is_overdue %}
                                        <br><small class="text-danger"><i class="fas fa-exclamation-triangle"></i> 超期</small>
                                    {% endif %}
                                </td>
                                <td>
                                    <span class="priority-{{ transfer.priority }}">
                                        <i class="fas fa-flag"></i> {{ transfer.get_priority_display }}
                                    </span>
                                </td>
                                <td>
                                    <small>{{ transfer.created_at|date:"Y-m-d H:i" }}</small>
                                </td>
                                <td>
                                    <div class="btn-group" role="group">
                                        <a href="{% url 'assets:transfer_detail' transfer.pk %}" class="btn btn-sm btn-outline-primary">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                        {% if transfer.status == 'pending' and transfer.can_approve %}
                                            <a href="{% url 'assets:transfer_approve' transfer.pk %}" class="btn btn-sm btn-outline-success">
                                                <i class="fas fa-check"></i>
                                            </a>
                                        {% endif %}
                                        {% if transfer.can_execute %}
                                            <a href="{% url 'assets:transfer_execute' transfer.pk %}" class="btn btn-sm btn-outline-warning">
                                                <i class="fas fa-play"></i>
                                            </a>
                                        {% endif %}
                                    </div>
                                </td>
                            </tr>
                            {% empty %}
                            <tr>
                                <td colspan="10" class="text-center text-muted py-4">
                                    <i class="fas fa-inbox fa-3x mb-3"></i>
                                    <p>暂无调拨申请</p>
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
                
                <!-- 分页 -->
                {% if page_obj.has_other_pages %}
                <div class="d-flex justify-content-between align-items-center p-3 border-top">
                    <div class="text-muted">
                        显示第 {{ page_obj.start_index }} - {{ page_obj.end_index }} 条，共 {{ page_obj.paginator.count }} 条
                    </div>
                    <nav>
                        <ul class="pagination mb-0">
                            {% if page_obj.has_previous %}
                                <li class="page-item">
                                    <a class="page-link" href="?{% for key, value in request.GET.items %}{% if key != 'page' %}{{ key }}={{ value }}&{% endif %}{% endfor %}page={{ page_obj.previous_page_number }}">上一页</a>
                                </li>
                            {% endif %}
                            
                            {% for num in page_obj.paginator.page_range %}
                                {% if page_obj.number == num %}
                                    <li class="page-item active">
                                        <span class="page-link">{{ num }}</span>
                                    </li>
                                {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                                    <li class="page-item">
                                        <a class="page-link" href="?{% for key, value in request.GET.items %}{% if key != 'page' %}{{ key }}={{ value }}&{% endif %}{% endfor %}page={{ num }}">{{ num }}</a>
                                    </li>
                                {% endif %}
                            {% endfor %}
                            
                            {% if page_obj.has_next %}
                                <li class="page-item">
                                    <a class="page-link" href="?{% for key, value in request.GET.items %}{% if key != 'page' %}{{ key }}={{ value }}&{% endif %}{% endfor %}page={{ page_obj.next_page_number }}">下一页</a>
                                </li>
                            {% endif %}
                        </ul>
                    </nav>
                </div>
                {% endif %}
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
$(document).ready(function() {
    // 自动刷新功能
    setInterval(function() {
        // 只刷新统计数据，不刷新整个页面
        updateStats();
    }, 60000); // 每分钟刷新一次
    
    function updateStats() {
        // 这里可以通过AJAX更新统计数据
        // 暂时省略具体实现
    }
});
</script>
{% endblock %}